package com.tianqu.tms.entity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.UserDao;
import com.tianqu.tms.dao.ViewListDao;
import com.tianqu.tms.dto.UserDto;

/**
 * 
 * 用户表entity
 * 
 * @author tianshifu
 *
 */
public class User extends ViewListManager  {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//系统管理员
	public static final int SYSTEM_ADMINISTRATOR = 90;
	//操作员
	public static final int SYSTEM_OPERATER = 41;
	//会员
	public static final int SYSTEM_MEMBER = 11;
	/**
	 * User Class List
	 */
	/**
	 * 主键id
	 */
	private int id;
	/**
	 * 登录名
	 */
	private String loginId;
	/**
	 * 登录密码
	 */
	private String password;

	/**
	 * 用户真实姓名
	 */
	private String realName;

	/**
	 * 用户组id
	 */
	private String ucId;

	/**
	 * 操作者
	 */
	private int updateUserId;
	private int insertUserId;
	
	private Timestamp insertTime;// 受理时间/插入时间 
	private Timestamp updateTime;// 更新时间
	private String insertTimes;// 受理时间/插入时间 
	private String updateTimes;// 更新时间
	/**
	 * rownum
	 * 
	 * @return
	 */
	private int rowNum;
	//用户状态
	private int flag;
	private String flags;
	//样式
	private int cssFlag;
	//用户类型
	private String userType;

	public static User getInstance() {
		User user = new User();
		user.setExisting(false);
		return user;
	}

	/**
	 * 用户登录
	 * 
	 * @return User
	 * @throws TmsDaoException
	 */
	public static User getUserProfileByLoginid(String userId, String password)
			throws TmsDaoException {
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto userDto = uDao.findUserProfileByLoginid(userId);
		if (userDto == null) {
			return null;
		}
		
		User user = User.getInstance();
		user.setEntityFromDto(userDto);
		return user;
	}

	/**
	 * 从dto向entity赋值
	 * 
	 * @param userDto
	 */
	private void setEntityFromDto(UserDto userDto) {
		this.setId((int) userDto.getId());
		this.setLoginId(userDto.getUserName());
		this.setPassword(userDto.getPassword());
		this.setRealName(userDto.getRealName());
		this.setUcId(String.valueOf(userDto.getUcId()));
		this.setFlag(userDto.getFlag());
		this.setUserType(String.valueOf(userDto.getUserType()));
		this.setInsertTime(userDto.getInsertTime());

	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public void saveUserInfo(User user,int type) throws TmsDaoException {
		UserDto dto = new UserDto();
		dto.setUserName(user.getLoginId());
		dto.setRealName(user.getRealName());
		dto.setPassword(user.getPassword());
		dto.setUcId(Integer.parseInt(user.getUcId()));
		dto.setUpdateUserId(user.getUpdateUserId());
		dto.setInsertUserId(user.getInsertUserId());
		dto.setUserType(type);
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);

		uDao.saveUserInfo(dto);

//		setEntityFromDto(dto);
	}

	/**
	 * 查询出所有用户信息列表
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public List<User> getAllUserInfo(int userClass,int uniqueid) throws TmsDaoException {
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER,getPageNo(), getMaxIndex(), getSortKey(), isAsc());
		ViewListDao idao = (ViewListDao) TmsDao.getViewListDao((TmsDao) uDao,
				TmsDao.TMS_USER);
		
		List<UserDto> list = uDao.getAllUserInfo(userClass,uniqueid);
		List<User> userList = new ArrayList<User>();
		for (int i = 0; i < list.size(); i++) {
			UserDto dto = (UserDto) list.get(i);
			User entity = new User();
			entity.setRowNum(i + 1);
			entity.setId((int) dto.getId());
			entity.setLoginId(dto.getUserName());
			entity.setRealName(dto.getRealName());
			entity.setPassword(dto.getPassword());
			entity.setFlag(dto.getFlag());
			if(dto.getFlag()==1)
			{entity.setFlags("正常");}
			else if(dto.getFlag()==3)
			{entity.setFlags("禁用");}	
			entity.setInsertTimes(StringUtil.getTimeByTimeStamps(dto.getInsertTime()));
			entity.setUpdateTimes(StringUtil.getTimeByTimeStamps(dto.getUpdateTime()));
			UserClass code = UserClass.getInstance();
			entity.setUcId(code.getUserClassNameByUserClass(dto.getUcId()));
			if(i%2 == 0){
				entity.setCssFlag(1);
			}else{
				entity.setCssFlag(2);
			}
			userList.add(entity);
		}
		setPagingInfo(idao);
		return userList;
	}
	
	/**
	 * 用户判断用户唯一行
	 * @return
	 * @throws TmsDaoException 
	 */
	public boolean findUserInfoByUserId(String userId,int id) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto = uDao.findUserInfoByUserId(userId,id);
		if(dto == null)
		{
			return true;
		}
		return false;
		
	}
	
	/**
	 * 判断用户是否重复
	 * @return
	 * @throws TmsDaoException 
	 */
	public boolean isRepeatUserId(String userId) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		boolean result = uDao.isRepeatUserId(userId);
		return result;	
	}
	
	/**
	 * 根据主键ID查询出单条信息
	 * @return
	 * @throws TmsDaoException 
	 */
	public User getUserInfoById(long id) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto = uDao.findUserInfoById(id);
		User user = null;
		if(dto!=null)
		{
			user = new User();
			user.setEntityFromDto(dto);			
		}
		return user;
	}
	
	public Map<Long, UserDto> findAllUser() throws TmsDaoException {
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		Map<Long, UserDto> map = new HashMap<Long, UserDto>();
		List<UserDto> list = uDao.findAllUser();
		for (UserDto userDto : list) map.put(userDto.getId(), userDto);
		return map;
	}
	
	/**
	 *	根据ID判断原密码是否正确
	 * @return
	 * @throws TmsDaoException 
	 */
	public boolean getPasswordById(int id,String password) throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto = uDao.findPasswordById(id,password);
		if(dto == null)
		{
			return false;
		}
		return true;
	}
	/**
	 * 保存修改密码
	 * @return
	 * @throws TmsDaoException 
	 */
	public void savePwdInfo(String id,String password) throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		uDao.updatePasswordById(Integer.parseInt(id), password);
	}
	/**
	 * 保存修改密码
	 * @return
	 * @throws TmsDaoException 
	 */
	public void updatePasswordInfo(String id,String password,int updateUserId) throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		uDao.updatePasswordInfoById(Integer.parseInt(id), password,updateUserId);
	}
	/**
	 * 修改用户信息view.getId(),view.getLoginName(),view.getUserName(),view.getUserType()
	 * @return
	 * @throws TmsDaoException 
	 */
	public void updateUserInfoById(String id,int userType,int updateUserId) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto  = new UserDto();
		dto.setId(Long.parseLong(id));
		dto.setUcId(userType);
		dto.setUpdateUserId(updateUserId);
		uDao.updateUserInfoById(dto);
	}
	/**
	 * 根据主键ID删除用户信息
	 * @return
	 * @throws TmsDaoException 
	 */
	public void deleteUserInfoById(String id) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		uDao.deleteUserInfoById(Integer.parseInt(id));
	}
	/**
	 * 
	 * 根据userID更新帐号状态信息
	 * @return
	 * @throws TmsDaoException 
	 */
	public void updateLoginStatusByUserId(User user) throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto = new UserDto();
		dto.setUserName(user.getLoginId());
		dto.setFlag(user.getFlag());
		dto.setUpdateUserId(user.getUpdateUserId());
		uDao.updateLoginStatusByUserId(dto);
	}

	
	public void updatePasswordByUserId(String password,String userId,int updateUserId) throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		uDao.updatePasswordByUserId(password, userId,updateUserId);
	}
	
	/***
	 * 查询所有状态为1的用户信息
	 */
	public List<User> findIpadUser() throws TmsDaoException {
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		List<UserDto> list = uDao.findIpadUser();
		List<User> listUser = new ArrayList<User>();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				UserDto useDto = list.get(i);
				User user = new User();
				user.setLoginId(useDto.getUserName());
				user.setPassword(useDto.getPassword());
				user.setRealName(useDto.getRealName());
				listUser.add(user);
			}
		}
		return listUser;
	}
	
	
	/**
	 * 根据class的id查找用户
	 * @return
	 * @throws TmsDaoException
	 */
	@SuppressWarnings("unchecked")
	public List findUserByUcId(int ucId)throws TmsDaoException{
		List userDtoList = new ArrayList();
		List userList = new ArrayList();
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		userDtoList = uDao.findUserByUcId(ucId);
		for(int i=0;i<userDtoList.size();i++){
			UserDto dto = (UserDto)userDtoList.get(i);
			User user = new User(); 
			user.setId((int) dto.getId());
			user.setLoginId(dto.getUserName());
			user.setPassword(dto.getPassword());
			user.setRealName(dto.getRealName());
			user.setUcId(String.valueOf(dto.getUcId()));
			user.setFlag(dto.getFlag());
			userList.add(user);
			
		}
		return userList;
	}
	public List findCyUserByUcId(int ucId,int roleId)throws TmsDaoException{
		List userDtoList = new ArrayList();
		List userList = new ArrayList();
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		userDtoList = uDao.findCyUserByUcId(ucId,roleId);
		for(int i=0;i<userDtoList.size();i++){
			UserDto dto = (UserDto)userDtoList.get(i);
			User user = new User(); 
			user.setId((int) dto.getId());
			user.setLoginId(dto.getUserName());
			user.setPassword(dto.getPassword());
			user.setRealName(dto.getRealName());
			user.setUcId(String.valueOf(dto.getUcId()));
			user.setFlag(dto.getFlag());
			userList.add(user);
			
		}
		return userList;
	}
	/**
	 * 会员和经办人更新登录信息
	 * @return
	 * @throws TmsDaoException 
	 */
	public void updateLoginInfoAtAttn(User entity) throws TmsDaoException{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		UserDto dto = new UserDto();
		dto.setUserName(entity.getLoginId());
		dto.setRealName(entity.getRealName());
		dto.setUpdateUserId(entity.getUpdateUserId());
		uDao.updateLoginInfoAtAttn(dto);
	}
	public List getUserListByRole()throws TmsDaoException
	{
		UserDao uDao = (UserDao) TmsDao.getInstance(TmsDao.TMS_USER);
		List<UserDto> dtoList = uDao.getUserListByRole();
		List list = null;
		if(dtoList!=null)
		{
			list = new ArrayList();
			for(int i=0;i<dtoList.size();i++)
			{
				UserDto dto =(UserDto)dtoList.get(i);
				Map map = new HashMap();
				map.put("userid", dto.getId());
				map.put("userName", dto.getRealName());
				list.add(map);
			}
		}
		return list;
	}
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public int getUpdateUserId() {
		return updateUserId;
	}

	public void setUpdateUserId(int updateUserId) {
		this.updateUserId = updateUserId;
	}

 
	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public String getUcId() {
		return ucId;
	}

	public void setUcId(String ucId) {
		this.ucId = ucId;
	}

	public int getCssFlag() {
		return cssFlag;
	}

	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public int getInsertUserId() {
		return insertUserId;
	}

	public void setInsertUserId(int insertUserId) {
		this.insertUserId = insertUserId;
	}

	public Timestamp getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Timestamp insertTime) {
		this.insertTime = insertTime;
	}

	public Timestamp getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Timestamp updateTime) {
		this.updateTime = updateTime;
	}

	public String getFlags() {
		return flags;
	}

	public void setFlags(String flags) {
		this.flags = flags;
	}

	public String getInsertTimes() {
		return insertTimes;
	}

	public void setInsertTimes(String insertTimes) {
		this.insertTimes = insertTimes;
	}

	public String getUpdateTimes() {
		return updateTimes;
	}

	public void setUpdateTimes(String updateTimes) {
		this.updateTimes = updateTimes;
	}


}
